package com.hefeng.unit.controller;


import com.hefeng.common.core.utils.EmptyUtils;
import com.hefeng.common.core.web.domain.AjaxResult;
import com.hefeng.unit.controller.entity.ParamEntity;
import com.hefeng.unit.domain.GyDevice;
import com.hefeng.unit.service.GyDeviceService;
import com.hefeng.unit.service.GyDeviceSonService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.RequestBody;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description: 工业端设备控制层
 * @author： hzh
 * @datetime： 2022/11/29 17:05
 * @参数说明
 */
@Slf4j
@RestController
@RequestMapping("/unit/device")
@Api(tags = "设备信息表对象功能接口")
public class GYDeviceInfoContoller {
    @Autowired
    private GyDeviceService gyDeviceService;
    @Autowired
    private GyDeviceSonService gyDeviceSonService;

    /**
     * 获取设备信息
     *
     * @param gyDevice
     * @param
     * @param
     * @return fwb
     */
    @PostMapping(value = "/deviceQuantity", produces = "application/json;charset=UTF-8")
    @ApiOperation("获取设备详情")
    public AjaxResult slelct(@RequestBody GyDevice gyDevice) {

        return AjaxResult.success(gyDeviceService.paginQuery(gyDevice));

    }

    /**
     * 获取设备状态数量
     *
     * @param
     * @return fwb
     */
    @PostMapping(value = "/quantity", produces = "application/json;charset=UTF-8")
    @ApiOperation("获取设备状态数量")
    public AjaxResult quantity(@RequestBody ParamEntity param) {
        Map<String,Object> dataMap=new HashMap<>();
        List s= gyDeviceService.quantity(param.getUserId());
        Integer n= gyDeviceSonService.quantityT(param.getUserId());

        dataMap.put("报警故障信息",s);
        dataMap.put("设备总数",n);
        return AjaxResult.success(dataMap);
    }

    /**
     * 新增主机
     *
     * @param gyDevice
     * @return fwb
     */
    @RequestMapping(value = "/saveDevice",produces = "application/json;charset=UTF-8")
    @ApiOperation("新增主机")
    public AjaxResult saveDevice( @RequestBody GyDevice gyDevice) {
        Map<String, Object> data = gyDeviceService.insert(gyDevice);
        if (!data.get("make").equals(true)) {
            return AjaxResult.error(501, data.get("msg").toString());
        }
        return AjaxResult.success();
    }



    /**
     * 方法说明: 修改设备
     * @Param: [userLogin]
     * @return: io.renren.util.ResultUtil
     * @Author: fwb
     * @Date: 2022/2/16
     */
    @RequestMapping(value = "/updateDevice",produces = "application/json;charset=UTF-8")
    @ApiOperation("修改设备")
    public AjaxResult updateDevice(@RequestBody List<GyDevice> gyDevice){
        gyDeviceService.update(gyDevice);
        return AjaxResult.success("修改成功");
    }

    /**
     * 方法说明: 获取设备详情
     * @param paramEntity
     * @return null
     * @Author: fwb
     */
    @RequestMapping(value = "/detailsDevice",produces = "application/json;charset=UTF-8")
    @ApiOperation("获取设备详情")
    public AjaxResult detailsDevice(@RequestBody ParamEntity paramEntity){
        Map<String,Object> dataMap=new HashMap<>();
        if (EmptyUtils.isEmpty(paramEntity.getUserId())){
            return AjaxResult.error(500,"用户未登陆");
        }
        GyDevice device=gyDeviceService.getById(paramEntity.getDeviceId());
        dataMap.put("device",device);
        return AjaxResult.success("获取成功",dataMap);
    }


    /**
     * 新增独立式新增设备
     *
     * @param gyDevice
     * @return fwb
     */
    @RequestMapping( value = "/independent",produces = "application/json;charset=UTF-8")
    @ApiOperation("新增独立式新增设备")
    public AjaxResult independent(@RequestBody GyDevice gyDevice) {
        Map<String, Object> data = gyDeviceService.independent(gyDevice);
        if (!data.get("make").equals(true)) {
            return AjaxResult.error(501, data.get("msg").toString());
        }
        return AjaxResult.success();
    }


    /**
     * 方法说明:  删除设备
     * @param paramEntity
     * @return null
     * @Author: fwb
     */
    @RequestMapping(value = "/deleteDevice",produces = "application/json;charset=UTF-8")
    @ApiOperation("删除设备")
    public  AjaxResult deleteDevice(@RequestBody ParamEntity paramEntity){
        Map<String,Object> dataMap=new HashMap<>();
        dataMap=gyDeviceService.deleteById(paramEntity.getDeviceId());
        gyDeviceService.update(paramEntity.getDeviceId());
        gyDeviceSonService.update(paramEntity.getDeviceId());
        if(!dataMap.get("make").equals(true)){
            return AjaxResult.error(501,dataMap.get("msg").toString());
        }
        return AjaxResult.success("删除成功！");
    }

    /**
     * 主机列表
     * @param paramEntity
     * @return fwb
     */
    @RequestMapping(value = "/list",produces = "application/json;charset=UTF-8")
    @ApiOperation("主机设备列表")
    public  AjaxResult list(@RequestBody ParamEntity paramEntity){

        return AjaxResult.success(gyDeviceService.listT(paramEntity.getUserId(), paramEntity.getIsHost()));
    }
}
